<template>
  <text>{{ displayedText }}</text>
</template>

<script>
export default {
  name: 'TypewriterText',
  props: {
    text: {
      type: String,
      required: true
    },
    typingSpeed: {
      type: Number,
      default: 100 // 默认100毫秒间隔
    }
  },
  data() {
    return {
      displayedText: '',
      fullText: '',
      currentIndex: 0
    }
  },
  watch: {
    text: {
      immediate: true,
      handler(newText) {
        this.reset(newText)
      }
    }
  },
  methods: {
    reset(text) {
      this.fullText = text
      this.displayedText = ''
      this.currentIndex = 0
      this.typeNextCharacter()
    },
    typeNextCharacter() {
      if (this.currentIndex < this.fullText.length) {
        this.displayedText += this.fullText.charAt(this.currentIndex)
        this.currentIndex++
        setTimeout(this.typeNextCharacter, this.typingSpeed)
      }
    }
  }
}
</script> 